import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 指定 ChromeDriver 路径
driver_path = "D:/桌面/Application/chromedriver.exe"
service = Service(driver_path)

# 启动 WebDriver
driver = webdriver.Chrome(service=service)

# 访问目标网站
url = "http://bbs.itheima.com/"
driver.get(url)

# 等待页面加载完成
time.sleep(3)

# 签到按钮的 XPath
sign_in_xpath = '//*[@id="portal_block_417_content"]/div/div/a[2]'

try:
    # 等待签到按钮加载
    sign_in_button = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, sign_in_xpath))
    )

    # 判断是否可用（enabled）
    is_enabled = sign_in_button.is_enabled()

    # 判断是否可见（visible）
    is_displayed = sign_in_button.is_displayed()

    # 判断是否被选中（selected） -> 适用于 checkbox, radio，但部分按钮也可能支持
    is_selected = sign_in_button.is_selected()

    # 获取属性值
    title = sign_in_button.get_attribute("title")
    class_name = sign_in_button.get_attribute("class")
    href = sign_in_button.get_attribute("href")

    # 输出结果
    print(f"签到按钮可用: {is_enabled}")
    print(f"签到按钮可见: {is_displayed}")
    print(f"签到按钮被选中: {is_selected}")  # 如果适用于该按钮，否则返回 False
    print(f"签到按钮的 title: {title}")
    print(f"签到按钮的 class: {class_name}")
    print(f"签到按钮的 href: {href}")

except (NoSuchElementException, TimeoutException):
    print("未找到签到按钮，可能该元素未加载或 XPath 失效")

# 关闭浏览器
driver.quit()
